package com.cy.myblog.service.impl;

import com.cy.myblog.dao.ArticleDaoI;
import com.cy.myblog.model.Tarticle;
import com.cy.myblog.pageModel.Article;
import com.cy.myblog.pageModel.DataGrid;
import com.cy.myblog.service.ArticleServiceI;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("articleService")
public class ArticleServiceImpl implements ArticleServiceI {

	private ArticleDaoI articleDao;
	
	@Autowired
	public void setArticleDao(ArticleDaoI articleDao) {
		this.articleDao = articleDao;
	}

	public Tarticle save(Tarticle article) {
		articleDao.saveOrUpdate(article);
		return article;
	}

	@Override
	public Tarticle getById(String id) {
		return articleDao.getById(Tarticle.class,id);
	}

	@Override
	public List<Tarticle> findAll() {
		String hql = "from Tarticle order by releasetime desc";
		List<Tarticle> tas =  articleDao.find(hql);
		return tas;
	}

	public DataGrid findByPage(Article article){
		DataGrid data = new DataGrid();
		String hql = "from Tarticle t where 1=1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(article, hql, params);
		String hql_total = "select count(*) " + hql;
		hql = addOrder(article, hql);

		Long count = articleDao.count(hql_total,params);
		//分页
		List<Tarticle> tar = articleDao.find(hql,params,article.getPage(),article.getRows());
		List<Article>  ar = tarToArList(tar);
		data.setRows(ar);
		data.setTotal(count);
		return data;
	}

	@Override
	public void delete(Article article) {
		Tarticle tar = articleDao.getById(Tarticle.class,article.getId());
		articleDao.delete(tar);
	}

	@Override
	public void toTop(Article article) {
		Tarticle tar = getById(article.getId());
		if(article.getState() == 1) {
			tar.setState(2);
		}else if(article.getState() == 2){
			tar.setState(1);
		}else if(article.getState() == 0){
			tar.setState(1);
		}
	}

	public String addWhere(Article article,String hql,Map params){
		if (StringUtils.isNotBlank(article.getTitle())) {
			hql += " and t.title like :title ";
			params.put("title", "%" + article.getTitle() + "%");
		}
		if (StringUtils.isNotBlank(article.getClassifyname())) {
			hql += " and t.tclassify.classifyname like :classifyname ";
			params.put("classifyname", "%" + article.getClassifyname() + "%");
		}
		if (article.getStartdate() != null && article.getEnddate() != null){
			hql += " and t.releasetime between :startdate and :enddate ";
			params.put("startdate",article.getStartdate());
			params.put("enddate",article.getEnddate());
		}
		return hql;
	}

	private String addOrder(Article article, String hql) {
		if (article.getSort() != null && article.getOrder() != null) {
			hql += "order by " + article.getSort() + " " + article.getOrder();
		}else{
			hql += "order by state desc";
		}
		return hql;
	}

	public List<Article> tarToArList(List<Tarticle> tarticles) {
		List<Article> articles = new ArrayList<Article>();
        for(Tarticle tarticle : tarticles){
			Article article = new Article();
			BeanUtils.copyProperties(tarticle,article,new String[]{"content"});
			article.setClassifyname(tarticle.getTclassify().getClassifyname());
			articles.add(article);
		}
		return articles;
	}

	@Override
	public void update(Tarticle article) {
		articleDao.update(article);
	}
}
